<?php

class Administration extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		
		//vérifie le niveau de connexion
		if(isset($_SESSION['utilisateurRole']) && $_SESSION['utilisateurRole'] == 'administrateur')
		{
			$this->load->model('ActionsDeCommunication/ActionDeCommunication', 'ADC');
			$this->load->model('ActionsDeCommunication/Forum', 'Forum');
			$this->load->model('ActionsDeCommunication/Theme');
			$this->load->model('ActionsDeCommunication/Etat');
			$this->load->model('ActionsDeCommunication/Parametre');
			$this->load->model('Utilisateurs/Participe');
			$this->load->model('Utilisateurs/Enseignant');
			$this->load->model('Utilisateurs/Etudiants');
			
			include model_chemin('putDateJJMMAAAA');
			include model_chemin('putHoraireHHhMM');
			include model_chemin('fonctions');
			include_once model_chemin('get_annee_scolaire');
		}		
		else
			redirect(base_url().'index.php/welcome/erreur404');
	}
	
	public function tableauDeBord($action = null, $type = 'all', $filtre = null)
	{
		$data['url'] = 'index.php/Administration/tableauDeBord/';
		$data['tableauDeBord']=true;
		$data['erreur'] = $this->session->flashdata('erreur');
		$data['message'] = $this->session->flashdata('message');
		if($action != null)
		{		
			$data['titre'] = '';
			$this->load->view('Global/entete.php');
			
			if($action == 'all')
			{
				$data['actions'] = ActionDeCommunication::findAll();
				$this->load->view('ActionsDeCommunication/v_voirActions.php', $data);
			}
			elseif($action == 'adc')
			{
				$data['url'] = 'index.php/Administration/tableauDeBord/adc/'.$type.'/';
				if($filtre != null || isset($_SESSION['filtreAction']))
				{
					if($filtre == null)
						$filtre = $_SESSION['filtreAction'];
					$_SESSION['filtreAction'] = $filtre;
				}
				
				switch($type)
				{
					case 'incompletes':
						$data['titre'] = ': incompletes';
						$data['actions'] = ActionDeCommunication::findAll(ActionDeCommunication::findWhere('WHERE_INC'), $filtre);
						break;
					case 'ouvertes':
						$data['titre'] = ': ouvertes';
						$data['actions'] = ActionDeCommunication::findAll(ActionDeCommunication::findWhere('WHERE_OUV'), $filtre);
						break;
					case 'completes':
						$data['titre'] = ': inscriptions complètes';
						$data['actions'] = ActionDeCommunication::findAll(ActionDeCommunication::findWhere('WHERE_COM'), $filtre);
						break;
					case 'closes':
						$data['titre'] = ': inscriptions closes';
						$data['actions'] = ActionDeCommunication::findAll(ActionDeCommunication::findWhere('WHERE_CLO'), $filtre);
						break;
					case 'terminees':
						$data['titre'] = ': terminées';
						$data['actions'] = ActionDeCommunication::findAll(ActionDeCommunication::findWhere('WHERE_TER'), $filtre);
						break;
					case 'anoter':
						$data['titre'] = ': à noter';
						$data['actions'] = ActionDeCommunication::findAll(ActionDeCommunication::findWhere('WHERE_NOT'), $filtre);
						break;
					default:
						$data['actions'] = ActionDeCommunication::findAll(null, $filtre);
				}
				$this->load->view('ActionsDeCommunication/v_voirActions.php', $data);
			}				
			elseif($action == 'forum')
			{
				$data['url'] = 'index.php/Administration/tableauDeBord/forum/'.$type.'/';
				if($filtre != null || isset($_SESSION['filtreForum']))
				{
					if($filtre == null)
						$filtre = $_SESSION['filtreForum'];
					$_SESSION['filtreForum'] = $filtre;
				}
				
				switch($type)
				{
					case 'tous':
						$data['titre'] = '';
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_ALL'), $filtre);
						break;
					case 'incomplets':
						$data['titre'] = ': incomplets';
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_INC'), $filtre);
						break;
					case 'ouverts':
						$data['titre'] = ': ouverts';
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_OUV'), $filtre);
						break;
					case 'complets':
						$data['titre'] = ': inscriptions complètes';
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_COM'), $filtre);
						break;
					case 'clos':
						$data['titre'] = ': inscriptions closes';
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_CLO'), $filtre);
						break;
					case 'termines':
						$data['titre'] = ': terminés';
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_TER'), $filtre);
						break;
					case 'anoter':
						$data['titre'] = ': à noter';
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_NOT'), $filtre);
						break;
					default:
						$data['forums'] = Forum::findAll(null, Forum::findWhere('WHERE_ALL'), $filtre);
				}				
				$this->load->view('Forums/v_voirForums.php', $data);
			}	
			else
			{
				$data['actions'] = ActionDeCommunication::findAll();
				$this->load->view('ActionsDeCommunication/v_voirActions.php', $data);
			}		
			
			$this->load->view('Global/piedpage.php');
		}
		else
		{
			$data['allADC'] = $this->ADC->count(ActionDeCommunication::findWhere('WHERE_ALL'));
			$data['allForum'] = $this->Forum->count(Forum::findWhere('WHERE_ALL'));
			
			$data['incADC'] = $this->ADC->count(ActionDeCommunication::findWhere('WHERE_INC_COUNT'));
			$data['incForum'] = $this->Forum->count(Forum::findWhere('WHERE_INC'));
			
			$data['ouvADC'] = $this->ADC->count(ActionDeCommunication::findWhere('WHERE_OUV'));
			$data['ouvForum'] = $this->Forum->count(Forum::findWhere('WHERE_OUV'));
			
			$data['comADC'] = $this->ADC->count(ActionDeCommunication::findWhere('WHERE_COM'));
			$data['comForum'] = $this->Forum->count(Forum::findWhere('WHERE_COM'));	

			$data['cloADC'] = $this->ADC->count(ActionDeCommunication::findWhere('WHERE_CLO'));
			$data['cloForum'] = $this->Forum->count(Forum::findWhere('WHERE_CLO'));
			
			$data['terADC'] = $this->ADC->count(ActionDeCommunication::findWhere('WHERE_TER'));
			$data['terForum'] = $this->Forum->count(Forum::findWhere('WHERE_TER'));
			
			$data['notADC'] = $this->ADC->count(ActionDeCommunication::findWhere('WHERE_NOT'));
			$data['notForum'] = $this->Forum->count(Forum::findWhere('WHERE_NOT'));
			
			$this->load->library('session');
			
			$dateActionSession = $this->session->userdata('dateAction');
			
			if(isset($_POST['dateAction']))
			{
				if(verif($_POST['dateAction'], 'date'))
				{
					$d = substr($_POST['dateAction'], 0, 2);
					$m = substr($_POST['dateAction'], 3, 2);
					$y = substr($_POST['dateAction'], 6, 4);
					$dateAction = new DateTime($y.'-'.$m.'-'.$d);
					$this->session->set_userdata('dateAction', $dateAction);
				}
				else 
				{
					$this->session->set_flashData('erreurDateAction', 'Format de la date butoir des actions incorrect.');
					redirect(base_url().'index.php/Administration/tableauDeBord');	
				}			
			}
			elseif($dateActionSession == null)
			{
				$dateAction = new DateTime(null);
				$dateAction->add(date_interval_create_from_date_string('30 days'));
				$this->session->set_userdata('dateAction', $dateAction);
			}
			
			
			if(isset($_POST['dateForum']))
			{

				if(verif($_POST['dateForum'], 'date'))
				{
					$d = substr($_POST['dateForum'], 0, 2);
					$m = substr($_POST['dateForum'], 3, 2);
					$y = substr($_POST['dateForum'], 6, 4);
					$dateForum = new DateTime($y.'-'.$m.'-'.$d);
					$this->session->set_userdata('dateForum', $dateForum);
				}
				else
				{
					$this->session->set_flashData('erreurDateForum', 'Format de la date butoir des forums incorrect.');
					(base_url().'index.php/Administration/tableauDeBord');	
				}
			}
			else
			{
				$dateForum = new DateTime(null);
				$dateForum->add(date_interval_create_from_date_string('30 days'));
				$this->session->set_userdata('dateForum', $dateForum);
			}
			
			$data['dateJour'] = date('d/m/Y');
			
			$data['dateAction'] = $this->session->userdata('dateAction');
			$data['erreurDateAction'] = $this->session->flashdata('erreurDateAction');
			$data['actionsDate'] = ActionDeCommunication::findAllDate($data['dateAction']->format('Y-m-d'));
			
			$data['dateForum'] = $this->session->userdata('dateForum');
			$data['erreurDateForum'] = $this->session->flashdata('erreurDateForum');
			$data['forumsDate'] = Forum::findAllDate($data['dateForum']->format('Y-m-d'));
			
			$data['actionsPropose'] = ActionDeCommunication::findAll(ActionDeCommunication::findWhere('WHERE_PRO'));
			
			$this->load->view('Global/entete.php');
			$this->load->view('Administration/v_tableauDeBord.php', $data);
			$this->load->view('Global/piedpage.php');
		}
	}
	
	public function parametre()
	{
		$data['theme'] = Theme::findAll();
		$data['enseignant'] = Enseignant::findAll();
		$data['parametre'] = Parametre::findAll();
		$data['erreurParametre'] = $this->session->flashData('erreurParametre');
		$data['erreurTheme'] = $this->session->flashData('erreurTheme');
		$data['erreurEnseignant'] = $this->session->flashData('erreurEnseignant');
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_parametre.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function modificationParametre()
	{
		$parametre = Parametre::findAll();
		
		//met dans un tableau les valeurs des posts
		$parametrePost = array();
		$nbParametre = Parametre::count();
		
		for($i = 1; $i <= $nbParametre; $i++)
			$parametresPost[] = $_POST[$i];
		
		if($parametre != false && Parametre::check($parametresPost))
		{
			foreach($parametre as $p)
			{
				Parametre::modifyParametre($p->getId(), $_POST[$p->getId()]);
			}
			redirect(base_url().'index.php/Administration/parametre');
		}
		else
			redirect(base_url().'index.php/Administration/parametre');
	}
	
	public function creerTheme()
	{
		$data['modif'] = false;
		$data['message'] = $this->session->flashdata('message');
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_creerModifierTheme.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function creationTheme()
	{
		if(Theme::check($_POST['Libelle']))
		{
			Theme::addTheme(secur($_POST['Libelle'], 'string'));
			redirect(base_url().'index.php/Administration/parametre');
		}
		else
			redirect(base_url().'index.php/Administration/creerTheme');
	}
	
	public function modifierTheme($id)
	{
		$data['modif'] = true;
		$data['theme'] = Theme::findById(secur($id, 'int'));
		$data['message'] = $this->session->flashData('message');
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_creerModifierTheme.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function modificationTheme($id)
	{
		if(Theme::check($_POST['Libelle']))
		{
			Theme::modifyTheme(secur($id, 'int'), secur($_POST['Libelle'], 'string'));
			redirect(base_url().'index.php/Administration/parametre');
		}
		else
			redirect(base_url().'index.php/Administration/modifierTheme/'.$id);
	}
	
	public function suppressionTheme($id)
	{
		if($this->ADC->count(ActionDeCommunication::THEME_ADC.' = '.$id) == 0)
			Theme::delTheme(secur($id, 'int'));
		else
			$this->session->set_flashdata('erreurTheme', 'Impossible de supprimer, le thème est affecté à  des actions');
		redirect(base_url().'index.php/Administration/parametre');
	}
	
	public function creerEnseignant($login = null, $nom = null, $prenom = null, $mail = null)
	{
		$data['modif'] = false;
		$data['message'] = $this->session->flashData('message');
		$data['login'] = $this->session->flashData('login');
		$data['nom'] = $this->session->flashData('nom');
		$data['prenom'] = $this->session->flashData('prenom');
		$data['mail'] = $this->session->flashData('mail');
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_creerModifierEnseignant.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function creationEnseignant()
	{
		if(Enseignant::check($_POST['Login'], $_POST['Nom'], $_POST['Prenom'], $_POST['Mail']))
		{
			Enseignant::addEnseignant($_POST['Login'], secur($_POST['Nom'], 'string'), secur($_POST['Prenom'], 'string'), $_POST['Mail']);
			redirect(base_url().'index.php/Administration/parametre');
		}
		else 
		{
			$this->session->set_flashdata('login', $_POST['Login']);
			$this->session->set_flashdata('nom', $_POST['Nom']);
			$this->session->set_flashdata('prenom', $_POST['Prenom']);
			$this->session->set_flashdata('mail', $_POST['Mail']);
			redirect(base_url().'index.php/Administration/creerEnseignant');
		}
	}
	
	public function modifierEnseignant($login)
	{
		$data['modif'] = true;
		$data['enseignant'] = Enseignant::findByLogin((secur($login, 'string')));
		$data['message'] = $this->session->flashData('message');
		$data['login'] = $this->session->flashData('login');
		$data['nom'] = $this->session->flashData('nom');
		$data['prenom'] = $this->session->flashData('prenom');
		$data['mail'] = $this->session->flashData('mail');
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_creerModifierEnseignant.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function modificationEnseignant($login)
	{
		if(Enseignant::check($login, $_POST['Nom'], $_POST['Prenom'], $_POST['Mail'], 'modif'))
		{
			Enseignant::modifyEnseignant($login, secur($_POST['Nom'], 'string'), secur($_POST['Prenom'], 'string'), $_POST['Mail']);
			redirect(base_url().'index.php/Administration/parametre');	
		}
		else
		{
			$this->session->set_flashData('login', $login);
			$this->session->set_flashData('nom', $_POST['Nom']);
			$this->session->set_flashData('prenom', $_POST['Prenom']);
			$this->session->set_flashData('mail', $_POST['Mail']);
			redirect(base_url().'index.php/Administration/modifierEnseignant/'.$login);
		}
	}
	
	public function supprimerEnseignant($login)
	{
		$data['enseignant'] = Enseignant::findByLogin(secur($login, 'string'));
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_supprimerEnseignant.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function suppressionEnseignant($login)
	{
		$admin = unserialize($_SESSION['utilisateur']);
		if(secur($login, 'string') == $admin->getLogin())
			$this->session->set_flashdata('erreurEnseignant', 'Impossible de supprimer l\'administrateur');
		else
			Enseignant::delEnseignant(secur($login, 'string'));
		
		redirect(base_url().'index.php/Administration/parametre');
	}
	
	public function creerAction()
	{		
		$data['modif'] 			= false;
		$data['theme'] 			= Theme::findAll();
		$data['enseignant'] 	= Enseignant::findAll();
		$data['nbEtudiant'] 	= Parametre::findByLibelle('Nombre étudiants maximum');
		$data['noteMax']		= Parametre::findByLibelle('Nombre points maximum');
		$data['flash'] 			= $this->session->flashdata('message');
		$data['titre'] 			= $this->session->flashData('titre');
		$data['dateA'] 			= $this->session->flashData('date');
		$data['dateLimite'] 	= $this->session->flashData('dateLimite');
		$data['nbEtudiants'] 	= $this->session->flashData('nbEtudiants');
		$data['pointMax'] 		= $this->session->flashData('pointMax');
		$data['commentaire'] 	= $this->session->flashData('commentaire');
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_creerModifierADC.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function creationAction()
	{				
		if(isset($_POST['Lie']) && ActionDeCommunication::check($_POST['TitreAction'], 'null', 'null', 'null', 'null', 'null', $_POST['Theme'], 1))
		{
			ActionDeCommunication::addADC(secur($_POST['TitreAction'], 'string'), secur($_POST['Commentaire'], 'string'), null, null, null, null, null, $_POST['Theme'], 1, 1);
			redirect(base_url().'index.php/Administration/tableauDeBord');
		}
		elseif(ActionDeCommunication::check($_POST['TitreAction'], $_POST['Date'], $_POST['DateLimite'], $_POST['NbEtudiants'], $_POST['PointMaxA'], $_POST['Enseignant'], $_POST['Theme'], 0))
		{
			ActionDeCommunication::addADC(secur($_POST['TitreAction'], 'string'), secur($_POST['Commentaire'], 'string'), $_POST['Date'], $_POST['DateLimite'], $_POST['NbEtudiants'], $_POST['PointMaxA'], $_POST['Enseignant'], $_POST['Theme'], 1, 0);
			redirect(base_url().'index.php/Administration/tableauDeBord');
		}
		elseif(isset($_POST['Lie']))
		{
			$this->session->set_flashData('titre', $_POST['TitreAction']);
			$this->session->set_flashData('commentaire', $_POST['Commentaire']);
			redirect(base_url().'index.php/Administration/creerAction');
		}
		else 
		{
			$this->session->set_flashData('titre', $_POST['TitreAction']);
			$this->session->set_flashData('date', $_POST['Date']);
			$this->session->set_flashData('dateLimite', $_POST['DateLimite']);
			$this->session->set_flashData('nbEtudiants', $_POST['NbEtudiants']);
			$this->session->set_flashData('pointMax', $_POST['PointMaxA']);
			$this->session->set_flashData('commentaire', $_POST['Commentaire']);
			redirect(base_url().'index.php/Administration/creerAction');
		}
	}
	
	public function creerForum($id)
	{
		$data['modif'] 			= false;
		$data['action'] 		= ActionDeCommunication::findById(secur($id, 'int'));
		$data['noteMax'] 		= Parametre::findByLibelle('Nombre points maximum');
		$data['enseignant'] 	= Enseignant::findAll();
		$data['flash'] 			= $this->session->flashdata('message');
		$data['titre'] 			= $this->session->flashData('titre');
		$data['dateA'] 			= $this->session->flashData('date');
		$data['dateLimite'] 	= $this->session->flashData('dateLimite');
		$data['lieu'] 			= $this->session->flashData('lieu');
		$data['nom'] 			= $this->session->flashData('nom');
		$data['heureDeb'] 		= $this->session->flashData('heureDeb');
		$data['heureFin'] 		= $this->session->flashData('heureFin');
		$data['pointMax'] 		= $this->session->flashData('pointMax');
		$data['commentaire'] 	= $this->session->flashData('commentaire');
		$this->load->view('Global/entete.php');
		$this->load->view('Administration/v_creerModifierForum.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function creationForum($idAdc)
	{
		if(Forum::check(secur($idAdc, 'int'), $_POST['TitreForum'], $_POST['Date'], $_POST['DateLimite'], $_POST['LieuForum'], $_POST['HeureDeb'], $_POST['HeureFin'], $_POST['PointMaxF'], $_POST['Enseignant']))
		{
			Forum::addForum(secur($idAdc, 'int'), secur($_POST['TitreForum'], 'string'), secur($_POST['Commentaire'], 'string'), $_POST['Date'], $_POST['DateLimite'], secur($_POST['LieuForum'], 'string'), secur($_POST['NomLycee'], 'string'), $_POST['HeureDeb'], $_POST['HeureFin'], $_POST['PointMaxF'], $_POST['Enseignant'], 1);
			if($action->getTitre()==Forum::LUNDI || $action->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$idAdc);
			else
				redirect(base_url().'index.php/Forums/voirForums/'.$idAdc);
		}
		else 
		{
			$this->session->set_flashData('titre', $_POST['TitreForum']);
			$this->session->set_flashData('date', $_POST['Date']);
			$this->session->set_flashData('dateLimite', $_POST['DateLimite']);
			$this->session->set_flashData('lieu', $_POST['LieuForum']);
			$this->session->set_flashData('nom', $_POST['NomLycee']);
			$this->session->set_flashData('heureDeb', $_POST['HeureDeb']);
			$this->session->set_flashData('heureFin', $_POST['HeureFin']);
			$this->session->set_flashData('pointMax', $_POST['PointMaxF']);
			$this->session->set_flashData('commentaire', $_POST['Commentaire']);
			redirect(base_url().'index.php/Administration/creerForum/'.$idAdc);
		}
			
	}

	public function modifierAction($id)
	{
		$data['modif'] = true;
		$data['action'] = ActionDeCommunication::findById(secur($id, 'int'));
		$data['theme'] = Theme::findAll();
		$data['enseignant'] = Enseignant::findAll();
		$data['nbEtudiant'] = Parametre::findByLibelle('Nombre étudiants maximum');
		$data['noteMax'] = Parametre::findByLibelle('Nombre points maximum');
		$data['flash'] = $this->session->flashdata('message');
		if($data['action']->getEtat()->getLibelle() != 'Terminé' || $data['action']->getEtat()->getLibelle() != 'Noté')
		{
			$this->load->view('Global/entete.php');
			$this->load->view('Administration/v_creerModifierADC.php', $data);
			$this->load->view('Global/piedpage.php');
		}
		else
		{
			if($data['action']->getTitre()==Forum::LUNDI || $data['action']->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
			else
				redirect(base_url().'index.php/ActionsDeCommunication/detailAction/'.$id);
		}
	}
	
	public function modificationAction($id)
	{
		$action = ActionDeCommunication::findById(secur($id, 'int'));
		if($action->getGroupable() == 1 && ActionDeCommunication::check($_POST['TitreAction'], 'null', 'null', 'null', 'null', 'null', $_POST['Theme'], 1))
		{
			$action->modifyADC(secur($_POST['TitreAction'], 'string'), secur($_POST['Commentaire'], 'string'), null, null, null, null, null, $_POST['Theme'], $action->getEtat()->getId());
			redirect(base_url().'index.php/Forums/voirForums/'.$id);
		}
		elseif(ActionDeCommunication::check($_POST['TitreAction'], $_POST['Date'], $_POST['DateLimite'], $_POST['NbEtudiants'], $_POST['PointMaxA'], $_POST['Enseignant'], $_POST['Theme'], 0))
		{
			$action->modifyADC(secur($_POST['TitreAction'], 'string'), secur($_POST['Commentaire'], 'string'), $_POST['Date'], $_POST['DateLimite'], $_POST['NbEtudiants'], $_POST['PointMaxA'], $_POST['Enseignant'], $_POST['Theme'], $action->getEtat()->getId());
			if($action->getTitre()==Forum::LUNDI || $action->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
			else
				redirect(base_url().'index.php/ActionsDeCommunication/detailAction/'.$id);
		}
		else
			redirect(base_url().'index.php/Administration/modifierAction/'.$id);
	}
	
	public function modifierForum($id)
	{
		$data['modif'] = true;
		$data['forum'] = Forum::findById(secur($id, 'int'));
		$data['enseignant'] = Enseignant::findAll();
		$data['noteMax'] = Parametre::findByLibelle('Nombre points maximum');
		$data['flash'] = $this->session->flashdata('message');
		if($data['forum']->getEtat()->getLibelle() != 'Terminé' || $data['forum']->getEtat()->getLibelle() != 'Noté')
		{
			$this->load->view('Global/entete.php');
			$this->load->view('Administration/v_creerModifierForum.php', $data);
			$this->load->view('Global/piedpage.php');
		}
		else
		{
			if($data['forum']->getTitre()==Forum::LUNDI || $data['forum']->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
			else
				redirect(base_url().'index.php/Forums/detailForum/'.$id);
		}
	}
	
	public function modificationForum($id)
	{
		$forum = Forum::findById(secur($id, 'int'));
		if(Forum::check($forum->getAction()->getId(), $_POST['TitreForum'], $_POST['Date'], $_POST['DateLimite'], $_POST['LieuForum'], $_POST['HeureDeb'], $_POST['HeureFin'], $_POST['PointMaxF'], $_POST['Enseignant']))
		{
			$forum->modifyForum(secur($_POST['TitreForum'], 'string'), secur($_POST['Commentaire'], 'string'), $_POST['Date'], $_POST['DateLimite'], secur($_POST['LieuForum'], 'string'), secur($_POST['NomLycee'], 'string'), $_POST['HeureDeb'], $_POST['HeureFin'], $_POST['PointMaxF'], $_POST['Enseignant'], 1);
			if($forum->getTitre()==Forum::LUNDI || $forum->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
			else	
				redirect(base_url().'index.php/Forums/detailForum/'.$id);
		}
		else 
			redirect(base_url().'index.php/Administration/modifierForum/'.$id);
	}
	
	public function supprimerAction($id)
	{
		$data['action'] = ActionDeCommunication::findById(secur($id, 'int'));
		if($data['action']->getEtat()->getLibelle() != 'Terminé' || $data['action']->getEtat()->getLibelle() != 'Noté')
		{
			$this->load->view('Global/entete.php');
			$this->load->view('Administration/v_supprimerADC.php', $data);
			$this->load->view('Global/piedpage.php');
		}
		else
		{
			if($data['action']->getTitre()==Forum::LUNDI || $data['action']->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
			else
				redirect(base_url().'index.php/ActionsDeCommunication/detailAction/'.$id);
		}		
	}
	
	public function suppressionAction($id)
	{
		$action = ActionDeCommunication::findById(secur($id, 'int'));
		$action->delADC();
		redirect(base_url().'index.php/ActionsDeCommunication/voirActions');
	}
	
	public function supprimerForum($id)
	{
		$data['forum'] = Forum::findById(secur($id, 'int'));
		if($data['forum']->getEtat()->getLibelle() != 'Terminé' || $data['forum']->getEtat()->getLibelle() != 'Noté')
		{
			$this->load->view('Global/entete.php');
			$this->load->view('Administration/v_supprimerForum.php', $data);
			$this->load->view('Global/piedpage.php');
		}
		else
		{
			if($data['forum']->getTitre()==Forum::LUNDI || $data['forum']->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
			else
				redirect(base_url().'index.php/Forums/detailForum/'.$id);
		}
	}
	
	public function suppressionForum($id)
	{
		$forum = Forum::findById(secur($id, 'int'));
		$forum->delForum();
		if($forum->getTitre()==Forum::LUNDI || $forum->getTitre()==Forum::LUNDI2)
			redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$forum->getAction()->getId());
		else
			redirect(base_url().'index.php/Forums/voirForums/'.$forum->getAction()->getId());
	}
	
	public function noterAction($id) {
		$data['erreur'] = $this->session->flashdata('erreur');
		$data['action'] = ActionDeCommunication::findById(secur($id, 'int'));
		if($data['action']->getEtat()->getLibelle() == 'Terminé' || $data['action']->getEtat()->getLibelle() == 'Noté')
		{
			$data['etudiants'] = ActionDeCommunication::findEtudiants(secur($id, 'int'));			
			
			if($data['action']->getEtat()->getLibelle() == 'Noté')
				$data['notes'] = Participe::getNoteByAction(secur($id, 'int'));
			
			$this->load->view('Global/entete.php');
			$this->load->view('Administration/v_noterAction.php', $data);
			$this->load->view('Global/piedpage.php');
		}	
		else
		{
			if($data['action']->getTitre()==Forum::LUNDI || $data['action']->getTitre()==Forum::LUNDI2)
				redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
			else
				redirect(base_url().'index.php/ActionsDeCommunication/detailAction/'.$id);
		}
	}
	
	public function noterForum($id) {
		$data['erreur'] = $this->session->flashdata('erreur');
		$data['forum'] = Forum::findById(secur($id, 'int'));		
		if($data['forum']->getEtat()->getLibelle() == 'Terminé' || $data['forum']->getEtat()->getLibelle() == 'Noté')
		{
			if($data['forum']->getNoteEtu1() != null || $data['forum']->getNoteEtu2() != null)
				$data['modif'] = true;
			else
				$data['modif'] = false;
			
			$this->load->view('Global/entete.php');
			$this->load->view('Administration/v_noterForum.php', $data);
			$this->load->view('Global/piedpage.php');
		}
		else 
			redirect(base_url().'index.php/Forums/detailForum/'.$id);
	}
	
	public function notationAction($id) {
		$action = ActionDeCommunication::findById(secur($id, 'int'));
		$etudiants = ActionDeCommunication::findEtudiants(secur($id, 'int'));
		$erreur = false;
		if($action->getEtat()->getLibelle() == 'Terminé' || $action->getEtat()->getLibelle() == 'Noté')
		{
			foreach($etudiants as $etudiant)
			{
				if(isset($_POST[$etudiant->getLogin()]))
				{
					$note = $_POST[$etudiant->getLogin()];
					$participe = new Participe();
					$participe->setAction(secur($id, 'int'));
					$participe->setEtudiant($etudiant->getId());
					if(verif($note, 'note', $action->getNoteMax()))
						$participe->modifyNote($note);
					else
						$erreur = true;
				}
			}
		
			if(!$erreur)
			{
				$this->session->set_flashdata('message','Notation effectuée avec succès.');
				$action->modifyEtatADC(5);
			}
			else
			{
				$this->session->set_flashdata('erreur','La note doit être comprise entre -'.$action->getNoteMax().' et '.$action->getNoteMax());
				redirect(base_url().'index.php/Administration/noterAction/'.$id);	
			}			
		}
		if($action->getTitre()==Forum::LUNDI || $action->getTitre()==Forum::LUNDI2)
			redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$id);
		else
			redirect(base_url().'index.php/ActionsDeCommunication/detailAction/'.$id);
	}
	
	public function notationForum($id) {
		$forum = Forum::findById(secur($id, 'int'));
		if(isset($_POST['pointAttribue1']) && isset($_POST['pointAttribue2']) && $forum->getEtat()->getLibelle() == 'Terminé' || $forum->getEtat()->getLibelle() == 'Noté')
		{
			if(verif($_POST['pointAttribue1'], 'note', $forum->getNoteMax()) && verif($_POST['pointAttribue2'], 'note', $forum->getNoteMax()))
			{
				$forum->modifyNote($_POST['pointAttribue1'], $_POST['pointAttribue2']);
				$forum->modifyEtatForum(5);
				$this->session->set_flashdata('message','Notation effectué avec succès.');
			}
			else
			{
				$this->session->set_flashdata('erreur','La note doit être comprise entre -'.$forum->getNoteMax().' et '.$forum->getNoteMax());
				redirect(base_url().'index.php/Administration/noterForum/'.$id);
			}
		}
		redirect(base_url().'index.php/Forums/detailForum/'.$id);
	}
	
	public function exporterBase() {
		$this->load->database('db_adc', true);
		
		system("mysqldump --host=".$this->db->hostname." --user=".$this->db->username." --password=".$this->db->password." ".$this->db->database." > saveBdd/sauvegardeBddActionDeCom.sql");
		if(file_exists('saveBdd/sauvegardeBddActionDeCom.sql'))
			redirect(base_url().'saveBdd/sauvegardeBddActionDeCom.sql');
		else
			redirect(base_url().'index.php/Administration/parametre');	
	}
	
	public function importerBase() {	
		if($_FILES['file']['size'] > 100)
		{			
			if (move_uploaded_file($_FILES['file']['tmp_name'], "saveBdd/upload/sauvegardeBddActionDeCom.sql")) 
			{
				$this->load->database('db_adc', true);
				
				system("cat saveBdd/upload/sauvegardeBddActionDeCom.sql | mysql --host=".$this->db->hostname." --user=".$this->db->username." --password=".$this->db->password." ".$this->db->database);	
			}
		}
		redirect(base_url().'index.php/Administration/parametre');	
	}
	
	public function exporterAction($id)	{
		$action = ActionDeCommunication::findById(secur($id, 'int'));
		$chemin = 'saveBdd/actions/save'.cleanCaract($action->getTitre()).'.exp';
		$file = fopen($chemin, "w+");
		$insert = serialize($action);
		fwrite($file, $insert);
		fclose($file);
		redirect(base_url().$chemin);	
	}
}
?>
